/*
 * JFrameIdent.java
 *
 * Created on 4 juillet 2008, 09:09
 */

package GUI;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
/**
 *
 * @author  Coco
 */
public class LoginWindow extends javax.swing.JFrame {
    
    /** Creates new form JFrameIdent */
    public LoginWindow() {
        //on d�finit les parametres de la JFrameIdent
        this.setSize(335,137);
        this.setResizable(true);
        this.setTitle("Welcome to Rent-A-Supercar");
        this.setVisible(true);
        initComponents();
        this.setLocationRelativeTo(null);
        this.username.requestFocusInWindow(); //Focus sur le TextFieldIdentifiant
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new javax.swing.JLabel();
        usernameLabel = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        password = new javax.swing.JPasswordField();
        ok = new javax.swing.JButton();
        quit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        title.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        title.setText("Rent-A-Supercar");
        title.setFocusable(false);
        title.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        usernameLabel.setText("Benutzername");

        passwordLabel.setText("Passwort");

        password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordKeyPressed(evt);
            }
        });

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });
        ok.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                okKeyPressed(evt);
            }
        });

        quit.setText("Beenden");
        quit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, 295, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordLabel)
                            .addComponent(usernameLabel))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(username, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                            .addComponent(password)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(ok)
                                .addGap(16, 16, 16)
                                .addComponent(quit)))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ok, quit});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok)
                    .addComponent(quit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
// Quitter le Logiciel avec la croix
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        int retour = JOptionPane.showConfirmDialog(this,"Sind Sie sicher ? ", "Beenden",JOptionPane.OK_CANCEL_OPTION);
        if(retour==JOptionPane.OK_OPTION){
        System.exit(0);
        }
    }//GEN-LAST:event_formWindowClosing
//Quitter le logiciel par le bouton
    private void quitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitActionPerformed
        int retour = JOptionPane.showConfirmDialog(this,"Sind Sie sicher ? ", "Beenden",JOptionPane.OK_CANCEL_OPTION);
        if(retour==JOptionPane.OK_OPTION){
        System.exit(0);
        }
}//GEN-LAST:event_quitActionPerformed
//Valider en pressant "entrer" sur l'identifiant'
    private void okKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_okKeyPressed
if (evt.getKeyCode()==KeyEvent.VK_ENTER){
    String ID=null;
    ID=kernel.Login.Login(username.getText().toString(),password.getText().toString());
    System.out.println(ID);
if(!ID.equals("3"))
{
                            GraphicalUserInterface Frame=new GraphicalUserInterface();
                            Frame.setExtendedState(GraphicalUserInterface.MAXIMIZED_BOTH); // On �tend la fenetre au maximum
                            this.setVisible(false); // On cache cette frame
                            Frame.footerDateTime.setText(ID);
}
else 
{
                                    JOptionPane.showMessageDialog(null, "Ihr Benutzername oder ihr Passwort ist falsch!","Error",JOptionPane.ERROR_MESSAGE) ;
}
}
}//GEN-LAST:event_okKeyPressed
//Validation de l'entr�e quand on clique sur Valider'
    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed


    String ID=null;
    ID=kernel.Login.Login(username.getText().toString(),password.getText().toString());
    System.out.println(ID);
   
if(!ID.equals("3"))
{
                            GraphicalUserInterface Frame=new GraphicalUserInterface();
                            Frame.setExtendedState(GraphicalUserInterface.MAXIMIZED_BOTH); // On �tend la fenetre au maximum
                            this.setVisible(false); // On cache cette frame
                            Frame.footerDateTime.setText(ID);
}
else 
{
                                    JOptionPane.showMessageDialog(null, "Ihr Benutzername oder ihr Passwort ist falsch!","Error",JOptionPane.ERROR_MESSAGE) ;
}
    
}//GEN-LAST:event_okActionPerformed
//Validation de l'entr�e quand on presse la touche entr�e sur le Password Ident'
    private void passwordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordKeyPressed
if (evt.getKeyCode()==KeyEvent.VK_ENTER){
    String ID=null;
    ID=kernel.Login.Login(username.getText().toString(),password.getText().toString());
    System.out.println(ID);
if(!ID.equals("3"))
{
                            GraphicalUserInterface Frame=new GraphicalUserInterface();
                            Frame.setExtendedState(GraphicalUserInterface.MAXIMIZED_BOTH); // On �tend la fenetre au maximum
                            this.setVisible(false); // On cache cette frame
                            Frame.footerDateTime.setText(ID);
}
else 
{
                                    JOptionPane.showMessageDialog(null, "Ihr Benutzername oder ihr Passwort ist falsch!","Error",JOptionPane.ERROR_MESSAGE) ;
}
}
}//GEN-LAST:event_passwordKeyPressed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginWindow().setVisible(true);
            }
        });
    }
    public static String sessionID;
    public static String benutzername;
    
    static public boolean CanEditCarData;
    static public boolean CanManageUsers;
    static public boolean CanSeeStatistics;
    static public boolean CanSeeHistories;    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ok;
    private javax.swing.JPasswordField password;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JButton quit;
    private javax.swing.JLabel title;
    private javax.swing.JTextField username;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
    
}
